package com.xiepxcc.annotation.base.anno;

import org.springframework.util.ObjectUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = NonNullAnnotation.NonNullChecker.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(NonNullAnnotation.List.class)
public @interface NonNullAnnotation {
    /**
     * @Description: 错误提示
     */
    String message() default "{javax.validation.constraints.NotNull.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };


    class NonNullChecker implements ConstraintValidator<NonNullAnnotation,Object>
    {

        @Override
        public void initialize(NonNullAnnotation arg0)
        {
            System.out.println("NonNullChecker initialize");
        }

        @Override
        public boolean isValid(Object value, ConstraintValidatorContext context)
        {
            System.out.println("isValid "+value);
            if(ObjectUtils.isEmpty(value))
            {
                return false;
            }
            return true;
        }

    }

    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface List {
        NonNullAnnotation[] value();
    }
}
